package com.gyp.controller.login;

import com.gyp.dto.user.*;
import com.gyp.entity.SysMenu;
import com.gyp.entity.SysRole;
import com.gyp.model.UserContextHolder;
import com.gyp.model.common.PageInfo;
import com.gyp.model.common.Result;
import com.gyp.service.UserService;
import com.gyp.vo.base.RouterVo;
import com.gyp.vo.base.SysDictData;
import com.gyp.vo.base.UserInfoVO;
import com.gyp.vo.login.Oauth2TokenVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @param
 * @author xia
 * @Description: 登录相关
 * @date 2021/1/24
 */
@RestController
@Validated
@AllArgsConstructor
@RequestMapping("/user")
@Slf4j
@Api(tags = "登录相关")
public class LoginController {

    @Autowired
    private UserService userService;

    @Autowired
    private PasswordEncoder passwordEncoder;


    @PostMapping("/wx/login")
    @ApiOperation("微信公众号用户登录")
    public Result<Oauth2TokenVO> wxChatLogin(@RequestParam("code") String code) throws Exception{
        return Result.data(userService.wxChatLogin(code));
    }

    @Deprecated
    @PostMapping("/wx/code")
    @ApiOperation("微信公众号二维码获取[利用微信服务号能力，一年要300块钱]")
    public Result<String> getWeChatCode() throws Exception{
        return Result.data(userService.getWeChatCode());
    }

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public Result<String> userRegister(@RequestBody @Valid RegisterDTO param) {
        userService.userRegister(param);
        return Result.success("用户注册成功");
    }

    @PostMapping("/login")
    @ApiOperation("用户登录")
    public Result<Oauth2TokenVO> login(@RequestBody @Valid LoginDTO param) {
        return Result.data(userService.login(param));
    }

    @ApiOperation("查看token秘钥")
    @GetMapping("/token")
    public Result<String> getUserToken(){
        return Result.data(userService.getUserToken());
    }

    @ApiOperation("退出登录")
    @PostMapping("/logout")
    public Result<String> logout(HttpServletRequest request) {
        String token = request.getHeader("token");
        userService.logout(token);
        return Result.success("登出成功");
    }

    @PostMapping("/info")
    @ApiOperation("查看用户信息")
    public Result<UserInfoVO> userInfo() {
        return Result.data(userService.userInfo());
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @ApiOperation("路由信息")
    @GetMapping("getRouters")
    @PreAuthorize("@userPermissionConfig.hasRole(\"admin\")")
    public Result<List<RouterVo>> getRouters() {
        Integer userId = UserContextHolder.getUserId();
        List<RouterVo> menus = userService.selectMenuTreeByUserId(userId);
        return Result.data(menus);
    }

    @ApiOperation("菜单列表")
    @GetMapping("/menu/list")
    @PreAuthorize("@userPermissionConfig.hasRole(\"admin\")")
    public Result menulist(SysMenu menu) {
        List<SysMenu> menus = userService.selectMenuList(menu);
        return Result.data(menus);
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @ApiOperation("查询字典数据信息")
    @GetMapping(value = "/type/{dictType}")
    @PreAuthorize("@userPermissionConfig.hasRole(\"admin\")")
    public Result dictType(@PathVariable String dictType) {
        List<SysDictData> data = userService.selectDictDataByType(dictType);
        return Result.data(data);
    }

    @ApiOperation("查询角色信息")
    @PreAuthorize("@userPermissionConfig.hasRole(\"admin\")")
    @GetMapping("/role")
    public Result<PageInfo<SysRole>> list(RoleQueryDTO role) {
        return Result.data(userService.selectRoleList(role));
    }


}
